package com.inspur.myapplication.view;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.inspur.myapplication.R;
import com.inspur.myapplication.dao.CartsDao;
import com.inspur.myapplication.entity.UserBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.ArrayList;
import java.util.HashMap;

public class CartsActivity extends AppCompatActivity {
    private ArrayList arrayList;
    private boolean edit;
    private SimpleAdapter simpleAdapter;
    private TextView sumMoney;

    private HashMap getOneCartGoods(String goodsId){
        HashMap result=null;
        for(int i=0;i<arrayList.size();i++){
            HashMap hashMap=(HashMap)arrayList.get(i);
            if(hashMap.get("goodsId").equals(goodsId)){
                return hashMap;
            }
        }

        return result;
    }

    private float calMoney(){
        float result=0;
        for(int i=0;i<arrayList.size();i++){
            HashMap hashMap=(HashMap)arrayList.get(i);
            if(hashMap.get("sel").equals("1")){
                result+=Float.parseFloat(hashMap.get("goodsPrice").toString())*Float.parseFloat(hashMap.get("quantity").toString());
            }
        }

        return result;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_cart);

        sumMoney=(TextView)findViewById(R.id.cart_sum);
        CheckBox selAll=(CheckBox)findViewById(R.id.cart_selall);
        selAll.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                for(int i=0;i<arrayList.size();i++){
                    HashMap hashMap=(HashMap)arrayList.get(i);
                    if(b){
                        hashMap.put("sel","1");
                    }else {
                        hashMap.put("sel","0");
                    }
                }
                simpleAdapter.notifyDataSetChanged();
                sumMoney.setText(calMoney()+"");
            }
        });
        //获取数据
        CartsDao cartsDao=new CartsDao(this);
        arrayList=cartsDao.queryCarts(UserBean.currentUserName);

        simpleAdapter=new SimpleAdapter(this,arrayList,R.layout.layout_cart_row,new String[]{"goodsName","goodsPrice","goodsImg","quantity","goodsId","goodsId","goodsId"},new int[]{R.id.cart_detail_goodsname,R.id.cart_detail_price,R.id.cart_detail_iv,R.id.cart_detail_quantity,R.id.cart_detail_add,R.id.cart_detail_sub,R.id.cart_detail_chkbox});
        ListView listView=(ListView)findViewById(R.id.cart_lv);
        listView.setAdapter(simpleAdapter);

        final ImageLoader imageLoader=ImageLoader.getInstance();
        ImageLoaderConfiguration config=new ImageLoaderConfiguration.Builder(this).build();
        imageLoader.init(config);

        simpleAdapter.setViewBinder(new SimpleAdapter.ViewBinder() {
            @Override
            public boolean setViewValue(View view, final Object o, String s) {
                //setViewValue方法，对于每项数据，都会调用一次
                if(view.getId()==R.id.cart_detail_iv){
                    imageLoader.displayImage(o.toString(),(ImageView) view);
                    return true;//返回true,代表当前数据的展示已处理，不需要SimpleAdapter来负责
                }
                //判断加减按键
                if(view.getId()==R.id.cart_detail_add||view.getId()==R.id.cart_detail_sub){
                    if(edit){
                        view.setVisibility(View.VISIBLE);
                    }else {
                        view.setVisibility(View.INVISIBLE);
                    }

                    ((Button)view).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            String goodsId=o.toString();
                            HashMap oneCartGoods=getOneCartGoods(goodsId);
                            int quantity=Integer.parseInt(oneCartGoods.get("quantity").toString());
                            if(view.getId()==R.id.cart_detail_add){
                                quantity++;
                            }else {
                                quantity--;
                            }
                            oneCartGoods.put("quantity",quantity);
                            simpleAdapter.notifyDataSetChanged();
                        }
                    });

                    return true;
                }
                //判断复选框
                if(view.getId()==R.id.cart_detail_chkbox){
                    String goodsId=o.toString();//得到当前商品的ID
                    final HashMap oneCartGoods=getOneCartGoods(goodsId);//根据ID,得到这一行数据

                    //判断当前是否选中，进行钩选
                    if(oneCartGoods.get("sel").toString().equals("0")){
                        ((CheckBox)view).setChecked(false);
                    }else {
                        ((CheckBox)view).setChecked(true);
                    }

                    ((CheckBox)view).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            if(b){
                                oneCartGoods.put("sel","1");
                            }else {
                                oneCartGoods.put("sel","0");
                            }
                            //重新计算总金额
                            float money=calMoney();
                            sumMoney.setText(money+"");
                        }
                    });

                    return true;
                }
                return false;
            }
            //
        });
    }

    public void editClick(View view){
        edit=!edit;
        //刷新列表
        simpleAdapter.notifyDataSetChanged();
        if(!edit){
            ((Button)view).setText("编辑");
        }else {
            ((Button)view).setText("完成");
        }

    }
}
